package com.han.nettytestdemo.connector.communication;

import com.han.nettytestdemo.connector.netty.util.ByteUtil;

import java.io.UnsupportedEncodingException;

/**
 * 通讯帮助类
 */
public class SeialHelp {

    /**
     * 给命令计算效验码
     */
    public static String[] checkNum(String CMD[]) {
        try {
            StringBuffer sb = new StringBuffer();
            for (int j = 0; j < CMD.length; j++) {
                String s1 = CMD[j];
                sb.append(s1);
            }
            byte[] s;
            s = sb.toString().getBytes("US-ASCII");
            int ss = 2;
            for (int i = 0; i < s.length - 2; i++) {
                ss = ss + (int) s[i];
            }
            String sd = Integer.toHexString(ss);
            String outStr = sd.substring(sd.length() - 2).toUpperCase();
            char[] ch = outStr.toCharArray();
            CMD[CMD.length - 2] = String.valueOf(ch[0]);
            CMD[CMD.length - 1] = String.valueOf(ch[1]);
            String ch0 = "";
            if (String.valueOf(ch[0]).length() == 1) {
                ch0 = "0" + String.valueOf(ch[0]).toUpperCase();
            } else {
                ch0 = String.valueOf(ch[0]).toUpperCase();
            }
            String ch1 = "";
            if (String.valueOf(ch[1]).length() == 1) {
                ch1 = "0" + String.valueOf(ch[1]).toUpperCase();
            } else {
                ch1 = String.valueOf(ch[1]).toUpperCase();
            }

            String[] chStr = new String[]{ch0, ch1};
            CMD = ByteUtil.byteMerger(CMD, chStr);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return CMD;
    }


    /**
     * @param i        相距几列
     * @param baseCode 左右的基准设备名称
     * @return
     */
    public static String getnewCode(int i, String baseCode) {
        int length = baseCode.length();
        String str = baseCode.substring(2, length);
        str = String.valueOf(Integer.valueOf(str) + (i - 1) * 10);
        String newStr = appendStr(str);
        return newStr;
    }

    /**
     * 拼接str 根据string的长度
     *
     * @param str
     * @return
     */
    public static String appendStr(String str) {
        if (str.length() == 1) {
            return "00" + str;
        } else if (str.length() == 2) {
            return "0" + str;
        } else {
            return str;
        }
    }

    public static String[] getNewstrSz(String s[], String str) {
        if (str.length() == 1) {
            s[6] = String.valueOf(0);
            s[7] = String.valueOf(str);
        } else if (str.length() == 2) {
            s[6] = String.valueOf(str.substring(0, 1));
            s[7] = String.valueOf(str.substring(1, 2));
        } else {
            s[5] = String.valueOf(str.substring(0, 1));
            s[6] = String.valueOf(str.substring(1, 2));
            s[7] = String.valueOf(str.substring(2, 3));
        }
        return s;
    }

    public static String int2HexString(String str) {
        String ret = "";
        ret = Integer.toHexString(Integer.valueOf(str));
        String Upstr = ret.toUpperCase();
        return Upstr;
    }

    /**
     * 16进制转10进制
     * 仅限于密集架PLC设置参数查询使用
     *
     * @param str
     * @return
     */
    public static String hexstr2int(String str) {
        String ret = "";
        if (str != null && !str.equals("") && str.length() >= 11) {
            ret = str.substring(4, 9);
        }
        if (ret != null && !ret.equals("")) {
            while (true) {
                if (ret.indexOf("0") == 0) {
                    ret = ret.substring(1, ret.length());
                    continue;
                } else {
                    break;
                }
            }
            if (ret != null && !ret.equals("")) {
                try {
                    ret = Integer.valueOf(ret, 16).toString();

                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    ret = "";
                }

            }
        }
        return ret;
    }

    /**
     * 十进制转16进制数字
     *
     * @param str
     * @return
     */
    public static String strToHex(String str) {
        int i = Integer.valueOf(str);
        int start = i / 16;
        int end = i % 16;
        String hex;
        if (start == 0) {
            hex = String.valueOf(end);
        } else {
            hex = String.valueOf(start) + String.valueOf(end);
        }
        return hex;
    }

    /**
     * 判断查询的列号是在固定列的左边还是右边，得到相距几列
     *
     * @param gdlNumber
     * @param columnNumber
     * @return
     */
    public static int getApartNumber(int gdlNumber, int columnNumber) {
        if (gdlNumber > columnNumber) {
            return gdlNumber - columnNumber;
        } else if (gdlNumber < columnNumber) {
            return columnNumber - gdlNumber;
        } else {
            return 0;
        }
    }

    /**
     * 判断是左还是右
     *
     * @param gdlNumber
     * @param columnNumber
     * @return
     */
    public static String isLeftOrRight(int gdlNumber, int columnNumber) {
        if (gdlNumber > columnNumber) {
            return "left";
        } else if (gdlNumber < columnNumber) {
            return "right";
        } else {
            return "1";
        }
    }

    /**
     * 替换生成新的命令
     *
     * @param str
     * @param string
     * @return
     */
    public static String[] replaceStr(String str[], String string) {
        str[3] = string;
        return str;
    }


    /**
     * @param age 返回16进制命令码
     * @return float [0] wd,[1]sd,[2]pm2.5
     */
    public static float[] formatImsHcs(String age) {
        //010306 00F0 014A 0000 414A   010306 010E 012C E381
        float a[] = new float[]{0, 0, 0, 0};
        String pm = "0", sd = "0", wd = "0", tvoc = "0";
        if (age.length() == 38) {
            sd = age.substring(30, 34);
            wd = age.substring(26, 30);
            pm = age.substring(18, 22);
            tvoc = age.substring(14, 18);

        }
        int wds = Integer.valueOf(wd, 16);
        int sds = Integer.valueOf(sd, 16);
        int pms = Integer.valueOf(pm, 16);
        int tvocs = Integer.valueOf(tvoc, 16);
        a[0] = (float) wds / 100;
        a[1] = (float) sds / 100;
        a[2] = pms;
        a[3] = tvocs;
        return a;
    }

    /**
     * 字符串
     * 16进制转2进制
     *
     * @param hexString
     * @return
     */
    public static String hexString2binaryString(String hexString) {
        if (hexString == null || hexString.length() == 0)
            return null;
        String bString = "", tmp;
        for (int i = 0; i < hexString.length(); i++) {
            tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
            bString += tmp.substring(tmp.length() - 4);
        }
        return bString;
    }

    /**
     * 获取通道
     *
     * @param data
     * @return
     */
    public static int getTD(String data) {
        char[] chars = data.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '1') {
                return 16 - i;
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        System.out.println(getTD("00000000000000000000000000000010"));
    }

    /**
     * 字符串
     * 2进制转16进制
     *
     * @param bString
     * @return
     */
    public static String binaryString2hexString(String bString) {
        if (bString == null || bString.equals("") || bString.length() % 8 != 0)
            return null;
        StringBuffer tmp = new StringBuffer();
        int iTmp = 0;
        for (int i = 0; i < bString.length(); i += 4) {
            iTmp = 0;
            for (int j = 0; j < 4; j++) {
                iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);
            }
            tmp.append(Integer.toHexString(iTmp));
        }
        return tmp.toString();
    }
}
